package org.twndmv.it;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJBTransactionRolledbackException;
import javax.naming.InitialContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.junit.Test;
import org.twndmv.bo.Sex;
import org.twndmv.dto.PersonDTO;
import org.twndmv.dto.PhysicalDetailsDTO;
import org.twndmv.ejb.DmvTestUtilRemote;
import org.twndmv.ejb.PersonMgmtRemote;



public class DmvRollbackIT {
	private static Log log = LogFactory.getLog(DmvRollbackIT.class);
	private InitialContext jndi;

    private static final String dmvTestUtilJNDI = System.getProperty("jndi.name.dmvTestUtil",
            "twnDmvEAR/twnDmvEJB/DmvTestUtilEJB!org.twndmv.ejb.DmvTestUtilRemote");
    private static final String dmvPersonJNDI = System.getProperty("jndi.name.person",
            "twnDmvEAR/twnDmvEJB/PersonMgmtEJB!org.twndmv.ejb.PersonMgmtRemote");

    private DmvTestUtilRemote testUtil;
    private PersonMgmtRemote personManager;
    

	@Before
	public void setUp() throws Exception {
		log.debug("===== TestDmvRollbackIT.init =====");

	        jndi = new InitialContext();
	        jndi.lookup("/"); //do a quick comms check of JNDI

	        testUtil = (DmvTestUtilRemote)jndi.lookup(dmvTestUtilJNDI);
	        personManager = (PersonMgmtRemote)jndi.lookup(dmvPersonJNDI);
	       

	        assertNotNull(testUtil);
	        assertNotNull(personManager);
	}
	
	@Test
	public void testRollback() {
		log.info("===== testRollback =====");
		testUtil.resetAll();
		//test to make sure no people currently
		assertEquals(personManager.getAllPeople().size(), 0);
		List<PersonDTO> people = new ArrayList<PersonDTO>();
		
		for(int i = 0; i < 10; i++) {
		PersonDTO testPerson = new PersonDTO();
	    testPerson.setFirstName("Steve " + i);
	    testPerson.setMiddleName("Danger");
	    testPerson.setLastName("Jobs " + i);
	    PhysicalDetailsDTO testDetails = new PhysicalDetailsDTO();
	    testDetails.setHeight(64 + i);
	    testDetails.setWeight(180 + i);
	    testDetails.setSEX(Sex.MALE);
	    testPerson.setPhysicalDetails(testDetails);
	    people.add(testPerson);
		}
		assertEquals(people.size(), 10);
		
		//add a good list of people and see it works.
		personManager.addList(people);
		assertEquals(personManager.getAllPeople().size(), 10);
		List<PersonDTO> people2 = new ArrayList<PersonDTO>();
		
		for(int i = 0; i < 9; i++) {
			PersonDTO testPerson = new PersonDTO();
		    testPerson.setFirstName("Steve " + i);
		    testPerson.setMiddleName("Danger");
		    testPerson.setLastName("Jobs " + i);
		    PhysicalDetailsDTO testDetails = new PhysicalDetailsDTO();
		    testDetails.setHeight(64 + i);
		    testDetails.setWeight(180 + i);
		    testDetails.setSEX(Sex.MALE);
		    testPerson.setPhysicalDetails(testDetails);
		    people2.add(testPerson);
        }
		PersonDTO testPerson = new PersonDTO();
	    testPerson.setFirstName(null);
	    testPerson.setMiddleName("Danger");
	    testPerson.setLastName("Jobs " + 10);
	    PhysicalDetailsDTO testDetails = new PhysicalDetailsDTO();
	    testDetails.setHeight(65);
	    testDetails.setWeight(185);
	    testDetails.setSEX(Sex.MALE);
	    testPerson.setPhysicalDetails(testDetails);
	    people2.add(testPerson);
	    
		assertEquals(people2.size(), 10);
		try {
		  personManager.addList(people2);
		} catch(EJBTransactionRolledbackException re) {
			log.info("Additions rolled back!");
			assertNotNull(re);
		}
		
		//Show the transaction failed cuz the tenth person has no first name...
		//if it passed, there would be 20 but there's still 10
		assertEquals(personManager.getAllPeople().size(), 10);
	}
}
